package com.example.myapplication;

import android.Manifest;
import android.annotation.SuppressLint;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.util.SparseArray;
import android.widget.FrameLayout;
import android.widget.RadioGroup;
import android.widget.Toast;

import com.example.myapplication.activitys.BaseActivity;
import com.example.myapplication.tabFragments.HomeFragment;
import com.example.myapplication.tabFragments.MapFragment;
import com.example.myapplication.utils.GpsUtils;
import com.tbruyelle.rxpermissions2.Permission;
import com.tbruyelle.rxpermissions2.RxPermissions;

import io.reactivex.functions.Consumer;

public class MainActivity extends BaseActivity implements RadioGroup.OnCheckedChangeListener {

    SparseArray<Fragment> array;
    FrameLayout frameLayout;
    FragmentManager fm;
    FragmentTransaction transaction;
    RadioGroup radioGroup;

    public static String CityCode = "746";
    public static String CityNmae = "泰安";

    public static String KEY = "vPcwjHqfwyfPrxnjkP1vsyKFPC4OxQPCXCPLHtAjcBOY6aT2txaygpWzSpQTXBLC";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        initFragment();
        switchFragment(0);
        iniPermissions();

        if (!GpsUtils.isOPen(MainActivity.this)) {
            Toast.makeText(MainActivity.this, "请打开gps，否者会影响正常使用", Toast.LENGTH_SHORT).show();
        }
    }

    private void initView() {
        frameLayout = findViewById(R.id.main_fragment_layout);
        radioGroup = findViewById(R.id.group);
        radioGroup.setOnCheckedChangeListener(this);
    }

    @SuppressLint("CommitTransaction")
    private void initFragment() {
        fm = getSupportFragmentManager();
        transaction = fm.beginTransaction();
        HomeFragment homeFragment = new HomeFragment();
        MapFragment mapFragment = new MapFragment();
        array = new SparseArray<>(2);
        array.put(0, homeFragment);
        array.put(1, mapFragment);
        transaction.add(R.id.main_fragment_layout, homeFragment);
        transaction.add(R.id.main_fragment_layout, mapFragment);
        transaction.commitAllowingStateLoss();
    }

    private void switchFragment(int index) {
        transaction = fm.beginTransaction();
        transaction.setTransition(FragmentTransaction.TRANSIT_UNSET);
        for (int i = 0; i < array.size(); i++) {
            if (index == i) {
                transaction.show(array.get(index));
            } else {
                transaction.hide(array.get(i));
            }
        }
        transaction.commit();
    }

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        switch (checkedId) {
            case R.id.home_rb:
                switchFragment(0);
                break;
            case R.id.map_rb:
                switchFragment(1);
                break;
        }
    }

    @SuppressLint("CheckResult")
    private void iniPermissions() {
        RxPermissions rxPermissions = new RxPermissions(this);
        rxPermissions.requestEach(Manifest.permission.ACCESS_COARSE_LOCATION,
                Manifest.permission.ACCESS_FINE_LOCATION,
                Manifest.permission.WRITE_EXTERNAL_STORAGE,
                Manifest.permission.READ_EXTERNAL_STORAGE,
                Manifest.permission.READ_PHONE_STATE
        )
                .subscribe(new Consumer<Permission>() {
                    @Override
                    public void accept(Permission permission) throws Exception {

                    }
                });
    }
}
